package Application.Summoner;

import Application.Config.Config;
import Application.Exceptions.AppExceptions;
import Application.Util.StaticUtil;
import Application.MainMenu;
import Application.MethodObject.DefTings;
import Application.Others.FunnyJoke;
import Application.Widgets.ErrorReportWidget;
import Application.Widgets.FormattedTextFileds.TextFieldInteger;
import Application.Widgets.TextFileds.TextFiledDouble;
import Application.Widgets.TextFileds.TextFiledString;
import Application.Writer.XamlWriter;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;

public class ThingSummoner {

    public static void ThingsSummoner() {
        DefTings def = new DefTings();
        Config configs = Config.configReader();
        FunnyJoke funnyJoke = new FunnyJoke();
        String[] nope = new String[1];
        nope[0] = "null";

            StaticUtil.summonerFrame = new JFrame();
            JFrame mainFrame = StaticUtil.summonerFrame;
            mainFrame.setTitle("边缘世界ModXAML生成器");
            mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);

            ErrorReportWidget.errorReport.setLineWrap(true);
            ErrorReportWidget.errorReport.setFont(configs.fontGetter());
            ErrorReportWidget.errorReport.setBackground(Color.BLACK);
            ErrorReportWidget.errorReport.setForeground(Color.RED);
            ErrorReportWidget.errorReport.setSelectionColor(Color.BLUE);
            JScrollPane scrollPane_1 = new JScrollPane(ErrorReportWidget.errorReport);
            scrollPane_1.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
            scrollPane_1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
            JButton funnyJokes = new JButton("看看笑话");
            funnyJokes.setFont(configs.fontGetter());
            funnyJokes.addActionListener(new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    ErrorReportWidget.errorReport.append(funnyJoke.getJokes_1() + "\n" + "笑话来源于网络\n");
                }
            });

            JPanel mainPanel = new JPanel();
            BoxLayout Box_Y = new BoxLayout(mainPanel, BoxLayout.Y_AXIS);
            mainPanel.setLayout(Box_Y);
            JPanel errPanel = new JPanel();
            BoxLayout Box_x = new BoxLayout(errPanel, BoxLayout.X_AXIS);
            errPanel.setLayout(Box_x);
            errPanel.setBackground(Color.BLACK);
            JMenuBar mainMenuBar = new JMenuBar();
            JMenu mainMenu = new JMenu("选项");
            JMenu mn_fontSetter = new JMenu("字体");
            JMenuItem mi_fontSetter_MS_Song = new JMenuItem("宋体");
            JMenuItem mi_fontSetter_Imitation_Song_Dynasty = new JMenuItem("仿宋");
            JMenuItem mi_backMainMenu = new JMenuItem("返回主菜单");
            JMenuItem mi_close = new JMenuItem("关闭");

            mi_fontSetter_MS_Song.setFont(configs.font_1);
            mi_fontSetter_MS_Song.addActionListener(e -> {
                try {
                    configs.fontNumSetter(0);
                    Config.configWriter(configs);
                } catch (AppExceptions ex) {
                    String err = ex.ErrGetter();
                    ErrorReportWidget.errorReport.append(err + "\n");
                }
            });
            mn_fontSetter.add(mi_fontSetter_MS_Song);
            mi_fontSetter_Imitation_Song_Dynasty.addActionListener(e -> {
                try {
                    configs.fontNumSetter(1);
                    Config.configWriter(configs);
                } catch (AppExceptions ex) {
                    String err = ex.ErrGetter();
                    ErrorReportWidget.errorReport.append(err + "\n");
                }
            });
            mi_fontSetter_Imitation_Song_Dynasty.setFont(configs.font_2);
            mi_backMainMenu.setFont(configs.fontGetter());
            mi_backMainMenu.addActionListener(e -> {
                mainFrame.setVisible(false);
                StaticUtil.mainMenuFrame = null;
                MainMenu.main(nope);
            });
            mn_fontSetter.add(mi_fontSetter_Imitation_Song_Dynasty);
            mn_fontSetter.setFont(configs.fontGetter());
            mainMenu.add(mn_fontSetter);
            mainMenu.add(mi_backMainMenu);
            mainMenu.add(mi_close);
            mainMenu.setFont(configs.fontGetter());
            mi_close.setFont(configs.fontGetter());
            mi_close.addActionListener(e -> System.exit(0));
            mainMenuBar.add(mainMenu);

            JLabel lab_XAMLName = new JLabel("请键入文件名称");
            lab_XAMLName.setFont(configs.fontGetter());

            TextFiledString tf_XAMLName = new TextFiledString(def.XAMLName);
            tf_XAMLName.setWidgetName("文件名设置器");
            tf_XAMLName.setFont(configs.fontGetter());

            JLabel lab_label = new JLabel("请输入物品显示名");
            lab_label.setFont(configs.fontGetter());

            TextFiledString tf_label = new TextFiledString(def.label);
            tf_label.setWidgetName("物品显示名设置器");
            tf_label.setFont(configs.fontGetter());

            JLabel lab_defName = new JLabel("请输入物品注册名");
            lab_defName.setFont(configs.fontGetter());

            TextFiledString tf_defName = new TextFiledString(def.defName);
            tf_defName.setWidgetName("物品注册名设置器");
            tf_defName.setFont(configs.fontGetter());

            JLabel lab_description = new JLabel("请输入物品描述");
            lab_description.setFont(configs.fontGetter());

            JTextArea tf_description = new JTextArea(1, 20);
            tf_description.setLineWrap(true);
            tf_description.setFont(configs.fontGetter());

            JLabel lab_texPath = new JLabel("请输入材质路径");
            lab_texPath.setFont(configs.fontGetter());

            TextFiledString tf_texPath = new TextFiledString(def.texPath);
            tf_texPath.setWidgetName("物品材质路径设置器");
            tf_texPath.setFont(configs.fontGetter());

            JLabel lab_stackLimit = new JLabel("请输入堆叠数量");
            lab_stackLimit.setFont(configs.fontGetter());

            TextFieldInteger tf_stackLimit = new TextFieldInteger(20);
            tf_stackLimit.setWidgetName("堆叠数量设置栏");
            tf_stackLimit.setFont(configs.fontGetter());

            JCheckBox cb_useHitPoints = new JCheckBox("是否启用耐久");
            cb_useHitPoints.setSelected(true);
            cb_useHitPoints.setFont(configs.fontGetter());

            JLabel lab_maxHitPoints = new JLabel("请输入物品耐久");
            lab_maxHitPoints.setFont(configs.fontGetter());

            TextFieldInteger tf_maxHitPoints = new TextFieldInteger( 20);
            tf_maxHitPoints.setWidgetName("物品耐久设置设置栏");
            tf_maxHitPoints.setText(String.valueOf(def.maxHitPoints));
            tf_maxHitPoints.setFont(configs.fontGetter());


            cb_useHitPoints.addChangeListener(e -> {
                if (cb_useHitPoints.isSelected())
                {
                    lab_maxHitPoints.setVisible(true);
                    tf_maxHitPoints.setVisible(true);
                }
                else
                {
                    lab_maxHitPoints.setVisible(false);
                    tf_maxHitPoints.setVisible(false);
                }
            });

            JCheckBox cb_selectable = new JCheckBox("是否允许选中");
            cb_selectable.setSelected(true);
            cb_selectable.setFont(configs.fontGetter());

            JCheckBox cb_healthAffectsPrice = new JCheckBox("是否允许耐久度影响价格");
            cb_healthAffectsPrice.setFont(configs.fontGetter());

            JLabel lab_marketValue = new JLabel("请输入物品贸易价格");
            lab_marketValue.setFont(configs.fontGetter());

            TextFieldInteger tf_marketValue = new TextFieldInteger( 20);
            tf_marketValue.setWidgetName("物品贸易价格设置栏");
            tf_marketValue.setFont(configs.fontGetter());

            JLabel lab_mass = new JLabel("请输入物品质量");
            lab_mass.setFont(configs.fontGetter());

            TextFiledDouble tf_mass = new TextFiledDouble(def.mass);
            tf_mass.setWidgetName("物品质量设置器");
            tf_mass.setToolTipText("该值为一个双标浮点数（即带有小数位的数字，也就是Double类型），范围：0.1~1.0");
            tf_mass.setFont(configs.fontGetter());

            JLabel lab_flammability = new JLabel("请输入物品燃烧系数");
            lab_flammability.setFont(configs.fontGetter());

            TextFiledDouble tf_flammability = new TextFiledDouble(def.flammability);
            tf_flammability.setFont(configs.fontGetter());

            JLabel lab_deteriorationRate = new JLabel("请输入物品劣化系数");
            lab_deteriorationRate.setFont(configs.fontGetter());

            TextFiledDouble tf_deteriorationRate = new TextFiledDouble(def.deteriorationRate);
            tf_deteriorationRate.setFont(configs.fontGetter());

            JCheckBox cb_alwaysHaulable = new JCheckBox("是否可被携带");
            cb_alwaysHaulable.setSelected(true);
            cb_alwaysHaulable.setFont(configs.fontGetter());

            JCheckBox cb_drawGUIOverlay = new JCheckBox("是否在选中时加载GUI");
            cb_drawGUIOverlay.setSelected(true);
            cb_drawGUIOverlay.setFont(configs.fontGetter());

            JCheckBox cb_rotatable = new JCheckBox("是否允许旋转");
            cb_rotatable.setFont(configs.fontGetter());

            JLabel lab_pathCost = new JLabel("请输入物品减速系数");
            lab_pathCost.setFont(configs.fontGetter());

            TextFieldInteger tf_pathCost = new TextFieldInteger( 20);
            tf_pathCost.setFont(configs.fontGetter());

            JLabel lab_thingCategories = new JLabel("请输入物品储存区类别");
            lab_thingCategories.setFont(configs.fontGetter());

            JTextArea tf_thingCategories = new JTextArea(1, 20);
            tf_thingCategories.setLineWrap(true);
            tf_thingCategories.setText("Manufactured");
            tf_thingCategories.setFont(configs.fontGetter());

            JLabel lab_tag = new JLabel("请输入物品标签");
            lab_tag.setFont(configs.fontGetter());

            JTextArea tf_tag = new JTextArea(1, 20);
            tf_tag.setLineWrap(true);
            tf_tag.setFont(configs.fontGetter());

            JLabel lab_thingSetMakerTags = new JLabel("请输入物品品质奖励标准");
            lab_thingSetMakerTags.setFont(configs.fontGetter());

            JTextArea tf_thingSetMakerTags = new JTextArea(1, 20);
            tf_thingSetMakerTags.setLineWrap(true);
            tf_thingSetMakerTags.setFont(configs.fontGetter());

            JButton choice_save_path = new JButton("请选择默认打开路径");
            choice_save_path.setFont(configs.fontGetter());
            choice_save_path.addActionListener(new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JFileChooser path_chooser = new JFileChooser();
                    path_chooser.setFont(configs.fontGetter());
                    path_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                    path_chooser.setDialogTitle("选择");
                    path_chooser.setAcceptAllFileFilterUsed(false);
                    path_chooser.addChoosableFileFilter(new FileFilter() {
                        @Override
                        public boolean accept(File f) {
                            return f.isDirectory();
                        }

                        @Override
                        public String getDescription() {
                            return "目录";
                        }
                    });
                    path_chooser.showOpenDialog(mainFrame);
                    configs.default_open_path = path_chooser.getSelectedFile();
                }
            });

            JButton summon = new JButton();
            summon.setVisible(true);
            summon.setText("单击以生成XAML文件");
            summon.setSize(100, 30);
            summon.setFont(configs.fontGetter());
            summon.addActionListener(e -> {
                def.XAMLName = tf_XAMLName.getValue();
                def.defName = tf_defName.getValue();
                def.label = tf_label.getValue();
                if (!tf_description.getText().equals("") && tf_description.getText() != null) {
                    def.description = tf_description.getName();
                }
                def.texPath = tf_texPath.getValue();
                def.stackLimit = tf_stackLimit.getInteger(def.stackLimit);
                def.useHitPoints = cb_useHitPoints.isSelected();
                if (cb_useHitPoints.isSelected()) {
                    def.maxHitPoints = tf_maxHitPoints.getInteger(def.maxHitPoints);
                }
                def.selectable = cb_selectable.isSelected();
                def.healthAffectsPrice = cb_healthAffectsPrice.isSelected();
                def.marketValue = tf_marketValue.getInteger(def.marketValue);
                def.mass = tf_mass.getValue(0,1);
                def.flammability = tf_flammability.getValue(0);
                def.deteriorationRate = tf_deteriorationRate.getValue(0,10);
                def.alwaysHaulable = cb_alwaysHaulable.isSelected();
                def.drawGUIOverlay = cb_drawGUIOverlay.isSelected();
                def.rotatable = cb_rotatable.isSelected();
                def.pathCost = tf_pathCost.getInteger(def.pathCost);
                if (!tf_thingCategories.getText().equals("") && tf_thingCategories.getText() != null) {
                    def.thingCategories = tf_thingCategories.getText();
                }
                if (!tf_tag.getText().equals("") && tf_tag.getText() != null) {
                    def.tags = tf_tag.getText();
                }
                if (!tf_thingSetMakerTags.getText().equals("") && tf_thingSetMakerTags.getText() != null) {
                    def.thingCategories = tf_thingSetMakerTags.getText();
                }

                JFileChooser chooser = new JFileChooser();
                chooser.setFont(configs.fontGetter());
                chooser.setDialogTitle("保存至");
                chooser.setAcceptAllFileFilterUsed(false);
                chooser.setSelectedFile(new File(def.XAMLName + ".xml"));
                chooser.setCurrentDirectory(configs.default_open_path);
                chooser.addChoosableFileFilter(new FileFilter() {
                    @Override
                    public boolean accept(File f) {
                        if (f.isDirectory()) {
                            return true;
                        } else return f.getName().endsWith(".xml");
                    }

                    @Override
                    public String getDescription() {
                        return "XAML源文件(.xml)";
                    }
                });
                int result = chooser.showOpenDialog(mainFrame);
                if (result == JFileChooser.APPROVE_OPTION)
                {
                    try {
                        XamlWriter.ThingsWriter(def, chooser.getSelectedFile());
                    } catch (IOException ex) {
                        ErrorReportWidget.errorReport.append(ex.getMessage());
                    }
                }
                else
                {
                    ErrorReportWidget.errorReport.append("AI:已取消文件输出！\n");
                }
            });

            mainPanel.add(lab_XAMLName);
            mainPanel.add(tf_XAMLName);
            mainPanel.add(lab_label);
            mainPanel.add(tf_label);
            mainPanel.add(lab_defName);
            mainPanel.add(tf_defName);
            mainPanel.add(lab_description);
            mainPanel.add(tf_description);
            mainPanel.add(lab_texPath);
            mainPanel.add(tf_texPath);
            mainPanel.add(lab_stackLimit);
            mainPanel.add(tf_stackLimit);
            mainPanel.add(cb_useHitPoints);
            mainPanel.add(lab_maxHitPoints);
            mainPanel.add(tf_maxHitPoints);
            mainPanel.add(cb_selectable);
            mainPanel.add(cb_healthAffectsPrice);
            mainPanel.add(lab_marketValue);
            mainPanel.add(tf_marketValue);
            mainPanel.add(lab_mass);
            mainPanel.add(tf_mass);
            mainPanel.add(lab_flammability);
            mainPanel.add(tf_flammability);
            mainPanel.add(lab_deteriorationRate);
            mainPanel.add(tf_deteriorationRate);
            mainPanel.add(cb_alwaysHaulable);
            mainPanel.add(cb_drawGUIOverlay);
            mainPanel.add(cb_rotatable);
            mainPanel.add(lab_pathCost);
            mainPanel.add(tf_pathCost);
            mainPanel.add(lab_thingCategories);
            mainPanel.add(tf_thingCategories);
            mainPanel.add(lab_tag);
            mainPanel.add(tf_tag);
            mainPanel.add(lab_thingSetMakerTags);
            mainPanel.add(tf_thingSetMakerTags);
            mainPanel.add(choice_save_path);
            mainPanel.add(summon);
            mainPanel.add(funnyJokes);

            errPanel.add(scrollPane_1, BorderLayout.WEST);

            mainFrame.setSize(1200, 500);
            mainFrame.add(mainPanel, BorderLayout.CENTER);
            mainFrame.add(errPanel, BorderLayout.EAST);
            mainFrame.setJMenuBar(mainMenuBar);
            mainFrame.setVisible(true);
        }
    }

